package com.xuecheng.framework.utils;

import com.github.pagehelper.PageInfo;
import org.apache.commons.collections4.CollectionUtils;
import java.util.List;


/**
 * @author KeHengCheng
 * @version V1.0
 * @ClassName PageUtils
 * @Description (这里用一句话描述这个类的作用)
 * @date 2019/9/11 17:43
 * @since V1.0
 */
public class PageUtils<T> {

    /*
     * @Description 对分页数据重新组装
     * @author KeHengCheng
     * @date 2019/9/11
     * @param  * @param list
     * @param pageNum
     * @param pageSize
     * @return com.github.pagehelper.PageInfo<T>
     */
    public PageInfo<T> getPageList(List<T> list, int pageNum, int pageSize){
        PageInfo<T> page = new PageInfo<>();
        page.setPageNum(pageNum);
        page.setPageSize(pageSize);

        if (CollectionUtils.isEmpty(list)){
            page.setSize(0);
            page.setTotal(0);
            page.setPages(0);
            page.setList(null);
            return page;
        }
        page.setTotal(list.size());
        //从满足条件的集合中取满足分页条件的数据
        int fromIndex = (pageNum-1)*pageSize;//起始位置
        int toIndex = (list.size()-fromIndex<pageSize?list.size():pageSize*pageNum);
        List<T> subList = list.subList(fromIndex,toIndex);
        //当前页的数量
        page.setSize(subList.size()-fromIndex<pageSize?(subList.size()-fromIndex):pageSize);
        int i = (int) Math.ceil((double) list.size()/pageSize);
        page.setPages(i);
        page.setList(subList);
        return page;
    }
}
